package com.codechuan.demo.thread;

import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.CityInfo;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.HttpControllerUtil;

import java.io.IOException;

/**
 * 生成订单线程
 */
public class ObtainOrderThread implements Runnable {
    SynContainer synContainer;

    public ObtainOrderThread(SynContainer synContainer) {
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();

        // 小号循环列表
        int arraySize = Constant.SON_TOKENS.size();
        int xc_id_size = Constant.xc_id.size();

        // 当前循环的位置
        int count = 0;
        // 当前循环行程位置
        int cont_xz_id = 0;

        while (Constant.isStart) {

            if (count == arraySize)
                count = 0;
            if (cont_xz_id == xc_id_size)
                cont_xz_id = 0;
            String token = "";
            if (Constant.MY_SON_TOKENS.size() > 0) {
                token = Constant.SON_TOKENS.get(count).getToken();
            }
            try {
                // 判断如果有小号就使用小号刷新
                if (!Constant.MY_SON_TOKENS.isEmpty()) {
                    if (!Constant.MAIN_TOKEN.equals(token)) {
                        System.out.println(name + "纯小号刷新开始启动[" + token + "]");
                        if (Constant.APP_IS_CHECK && !Constant.XC_ORDER) {
                            HttpControllerUtil.getWeChatAppProgramOrderList(Constant.cityInfo, synContainer, name, count);
                        } else {
                            if (Constant.APP_IS_CHECK && Constant.XC_ORDER){
                                HttpControllerUtil.getWeChatAppXcOrderList(Constant.xc_id.get(cont_xz_id), Constant.cityInfo, synContainer, name, count);
                            }
                            if (Constant.XCX_IS_CHECK && !Constant.XC_ORDER) {
                                HttpControllerUtil.getWeChatAppProgramOrderList(Constant.cityInfo, synContainer, name, count);
                            }
                            if (Constant.XCX_IS_CHECK && Constant.XC_ORDER){
                                HttpControllerUtil.getWeChatAppXcOrderList(Constant.xc_id.get(cont_xz_id), Constant.cityInfo, synContainer, name, count);
                            }
                        }
                    }
                } else {
                    System.out.println(name + "主号刷新开始启动[" + token + "]");
                    if (Constant.APP_IS_CHECK && !Constant.XC_ORDER) {
                        HttpControllerUtil.getWeChatAppProgramOrderList(Constant.cityInfo, synContainer, name, count);
                    } else {
                        if (Constant.APP_IS_CHECK && !Constant.XC_ORDER) {
                            HttpControllerUtil.getWeChatAppProgramOrderList(Constant.cityInfo, synContainer, name, count);
                        } else {
                            if (Constant.APP_IS_CHECK && Constant.XC_ORDER){
                                HttpControllerUtil.getWeChatAppXcOrderList(Constant.xc_id.get(cont_xz_id), Constant.cityInfo, synContainer, name, count);
                            }
                            if (Constant.XCX_IS_CHECK && !Constant.XC_ORDER) {
                                HttpControllerUtil.getWeChatAppProgramOrderList(Constant.cityInfo, synContainer, name, count);
                            }
                            if (Constant.XCX_IS_CHECK && Constant.XC_ORDER){
                                HttpControllerUtil.getWeChatAppXcOrderList(Constant.xc_id.get(cont_xz_id), Constant.cityInfo, synContainer, name, count);
                            }
                        }

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (count < arraySize)
                count++;
            if (cont_xz_id < xc_id_size)
                cont_xz_id++;
            // 判断小号不等于空且是大号直接跳出不需要等待
            if (!Constant.MY_SON_TOKENS.isEmpty() && Constant.MAIN_TOKEN.equals(token)) {
                System.out.println("小号中有大号不执行延时");
            } else {
                try {
                    Thread.sleep(Integer.parseInt(Constant.SX_NUMBER) * 10L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }
        System.out.println(name + "子线程关闭");
    }
}
